from django.http import JsonResponse
from comment.comviews import com_views, success_response, err_response, format_response
from utils.dbutils import DB_Config, MysqlTools
from utils.formatutils import get_params
from utils.pageutils import paging_simple
from comment.comservice import add_filter
from .sqlmap import *

mysql_tools = MysqlTools(db_config=DB_Config.get("default"))


def dept_tree(params):
    dict = {
        "user_id": "user_id"
    }
    params = get_params(params, dict)
    if isinstance(params, JsonResponse):
        return params
    # for key, val in params.items():
    #     if not val:
    #         return err_response(210)
    first = mysql_tools.select_one(TaskSqlMap.subject_top)
    if not first:
        return success_response({})
    second = mysql_tools.select_all(TaskSqlMap.subject_mid.format(**first))
    last = mysql_tools.select_all(TaskSqlMap.subject_down)
    for sec in second:
        sec["subordinate_dept"] = []
    for sec in second:
        for las in last:
            if sec["dept_id"] == las["last_dept_id"]:
                # las.pop("last_subject_id")
                sec["subordinate_dept"].append(las)
    first["subordinate_dept"] = second
    return success_response(first)


def dept_list(params):
    dict = {
        "user_id": "user_id"
    }
    page_params = {
        "isall": "isAll",
        "size": "limit",
        "page": "page",
        "query_str": "query_str"
    }
    params = get_params(params, dict, page_params)
    if isinstance(params, JsonResponse):
        return params
    # if not params["user_id"]:
    #     return err_response(210)
    sql = add_filter(TaskSqlMap.DEPT_LIST, params, ["query_str"], **TaskSqlMap.FILTER_PARAMS)
    sql = sql.format(**params)
    result = mysql_tools.select_all(sql)
    return success_response(result)


def my_dept(params):
    dict = {
        "user_id": "user_id",
    }
    params = get_params(params, dict)
    if isinstance(params, JsonResponse):
        return params
    # for key, val in params.items():
    #     if not val:
    #         return err_response(210)
    sql_user_dept = TaskSqlMap.user_dept.format(**params)
    my_dept = mysql_tools.select_one(sql_user_dept)
    return success_response(my_dept)


def dept_people(params):
    dict = {
        "dept_id": "dept_id",
    }
    page_params = {
        "isall": "isAll",
        "size": "limit",
        "page": "page",
        "query_str_name": "query_str"
    }
    params = get_params(params, dict, page_params)
    if isinstance(params, JsonResponse):
        return params
    # if not params.get("dept_id"):
    #     return err_response(210)
    sql_dept = TaskSqlMap.dept_people
    if params.get("dept_id") != 1:
        sql_dept += " where t1.dept_id={dept_id} or t1.dept_id=any(select dept_id from aoa_dept where children_id={dept_id})"
    sql = add_filter(sql_dept, params, ["query_str_name"], **TaskSqlMap.FILTER_PARAMS)
    sql_dept_people = sql.format(**params)
    dept_people = mysql_tools.select_all(sql_dept_people)
    if params.get("isall") != 1:
        dept_people = paging_simple(dept_people, params.get("page"), params.get("size"))
    if not dept_people:
        return success_response([])
    for res in dept_people:
        if res.get("hire_time"):
            res["hire_time"] = str(res.get("hire_time"))
    return success_response(dept_people)



@format_response
def index(request):
    return com_views(request, {
        "dept_tree": dept_tree,
        "my_dept": my_dept,
        "dept_people": dept_people,
        "dept_list": dept_list
    })
